package com.zzyl.serve.service.impl;

import java.util.Arrays;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-01
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
         return getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        nursingProject.setCreateTime(DateUtils.getNowDate());
        int i = save(nursingProject) ? 1 : 0;
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int i = updateById(nursingProject) ? 1 : 0;
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

//    /**
//     * 批量删除护理项目
//     *
//     * @param ids 需要删除的护理项目主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingProjectByIds(Long[] ids) {
//
//        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
//    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        List<NursingProjectPlan> list = nursingProjectPlanService.list();
        for (NursingProjectPlan nursingProjectPlan : list) {
            if(nursingProjectPlan.getProjectId() == id){
                return 0;
            }
        }
        NursingProject nursingProject = getById(id);
        if(nursingProject.getStatus() ==1){
            return 0;
        }
        int i = removeById(id) ? 1 : 0;
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

    /**
     * 获取项目列表
     * @return
     */
    @Override
    public List<NursingProjectVo> getProjectAll() {
        List<NursingProjectVo> nursingProjectVos = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(CollectionUtil.isNotEmpty(nursingProjectVos)){
            return nursingProjectVos;
        }
        nursingProjectVos=nursingProjectMapper.getProjectAll();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,nursingProjectVos);
        return nursingProjectVos;
    }
}
